// ==UserScript==
// @name           eRepublik Traditional Chinese Translation
// @namespace      Traditional Chinese Translation
// @description    eRepublik 正體中文版
// @version        0.0.1a
// @include        http://*.erepublik.com/*
// @require        http://sizzlemctwizzle.com/updater.php?id=80226&days=1&show
// ==/UserScript==

/* Note ^_^
  
  正規表示法            http://www.minwt.com/?p=1917
                        http://blog.roodo.com/rocksaying/archives/2670695.html
  js include file       http://www.minwt.com/?p=550
  jquery selectors      http://jquery.shian.tw/?m=selectors   http://api.jquery.com/category/selectors/
  auto update checker   http://userscripts.org/guides/45
  script加密            http://bit.ly/ccFnbo
  Introducing Using     http://bit.ly/ainyQe
  jQuery i18n Plugin    http://recursive-design.com/projects/jquery-i18n/
  Auto Translate        http://bit.ly/e7LADY
                        https://github.com/eligrey/l10n.js/#readme
						http://eligrey.com/blog/post/passive-localization-in-javascript
						
						
  
*/

//Jquery on Google API
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Main Common words 
////////////////////////////////////////////////////////////////////////////////////////////////////
var strings_main = {
// *Nations
// *Military Rank
// *Job Skill Rank
// *Mission
// *Mission
}
var regexps_main = {};

////////////////////////////////////////////////////////////////////////////////////////////////////
// economy.erepublik 
////////////////////////////////////////////////////////////////////////////////////////////////////

var strings_eco = {
// *Market Place
// *Job Market
// *Company for sale
// *Company Page
}
var regexps_eco = {};

////////////////////////////////////////////////////////////////////////////////////////////////////
// www.erepublik ----
////////////////////////////////////////////////////////////////////////////////////////////////////

var strings_www = {
// *Index
// *Profile
// *News
// *Party
// *Election
// *Ranks
// *Country-Administration
// *Invite Friends and other
// *Gold and Extras
// *Military
}
var regexps_www = {};

////////////////////////////////////////////////////////////////////////////////////////////////////
//Function
////////////////////////////////////////////////////////////////////////////////////////////////////

var subdomainname = location.href.match(/(\w+)\.(\w+)/)[1];

//去除字串左右空白
trim = function (str) {
    str = new String(str);return str!==null ? str.replace(/^\s*/, "").replace(/\s*$/, "") : null;
};

//更換regexps字串
matchRegexps = function(key) {
	var key = trim(key);
    key = new String(key);
	if (key===null) {
        return undefined;
    }
   
    for (var reg in regexps) {
        var rrrr = new RegExp(reg);	//var txt=new RegExp(pattern,modifiers);
        var result = key.match(rrrr);

        if (key.match(rrrr)!==null) {
            return key.replace(rrrr,regexps[reg]);	//string.replace(/\要尋找的字/g, '被取代的字');
        }
    }
    return undefined;
};

//翻譯字串("":"",) 
translateWithRegexp = function(key) {
    if (strings[key]!==undefined) {
        return strings[key];
    } else {
        var key2 = trim(key);
        if (strings[key2]!==undefined) {
        return strings[key2];
        }
    }
    return matchRegexps(key);
};

//tagName取得列表
var allTrans = {
	"span":"",
	"a":"",
	"h2":"","h3":"","h4":"","h5":"",
	"th":"","td":"",
	"p":"",
	"b":"",
	"small":"","big":"",
	"strong":"",
	"div":"",
	"label":"",
	"input":"",
	"li":"",
	"em":"",
	"option":"",
    };
	
	
translateWholePage = function(e) {

  var node = undefined;
  for (var tagName in allTrans) {
    var tags = document.getElementsByTagName(tagName);
    for (var key in tags) {
      node = tags[key];
      
	if ( node.tagName == "INPUT" && node.type == "submit" || node.type == "button" ||node.type == "text" )
      {
        var translation = translateWithRegexp(node.value);
        if (translation!==undefined) {
          node.value = translation;
        }
      }
      
      else if (node.childNodes) {
        if (node.childNodes.length<=3) {
          for (var i=0;i<node.childNodes.length;i++) {
            if (node.childNodes[i].nodeName=="#text") {
              translation = translateWithRegexp(node.childNodes[i].nodeValue);
              if (translation!==undefined) {
                node.childNodes[i].nodeValue = translation;
              }
            }
          }
        }	
      } else {
		var translation = translateWithRegexp(node.innerHTML);
        if (translation!==undefined) {
          node.innerHTML = translation;
        }
      }
    }
  }
}
//$(function(e) {translateWholePage(e);});

////////////////////////////////////////////////////////////////////////////////////////////////////
// Menu function ----
////////////////////////////////////////////////////////////////////////////////////////////////////
function menuff() {
	var lang = location.pathname.substr(1,2);
	var currentPage = location.href;
	var debugstr = "&nbsp;";
	// For debugger
	//$('#header').append('<div id="debugger"></div>');
	//$('#debugger').css({'width':'100%','background':'#00ffff'});
	//Add IRC
		//$('.at').after('<div id="eROC_IRC"></div>');
		//$('#eROC_IRC').html('<iframe src="http://qchat.rizon.net/?channels=PTT-Formosa&uio=d4" width="647" height="400"></iframe>');
	//Add eROC News or others
	if( currentPage.indexOf('battlefield') != "undefined" && currentPage.indexOf('battlefield') != -1 && currentPage.indexOf('battlefield') != null )	{		
			$('#pvp').eq(0).before('<div id="eROC_News"></div>');
		}else{
			$('#menu').eq(0).after('<div id="eROC_News"></div>');
		}
	$('#debugger').html(debugstr);
	$('#eROC_News').append('<div id="eROC_Military_Command"></div>');
	$('#eROC_News').append('<div id="eROC_legislative_News"></div>');
	$("#eROC_News").css({'width':'100%','background':'#ffffff'});
	$('#eROC_Military_Command').html('eROC最新軍令<br /><marquee id="eROC_Military_Command_list" onmouseover="this.stop()" onmouseout="this.start()" height="20" direction="up" scrolldelay="4" scrollamount="1"></marquee>');
	//遇到載入不完全或是timeout就會爛掉
	/*var militaryNews = $.get('/'+lang+'/newspaper/roc-ministry-of-defense-245452', function(data){
		var news = $('.articlecontent > .padded', data).contents();
		var newslist;
		newslist ="<ul>";
		for( var i=0;i<news.length;i++){
				newslist += '<li><a href="' + news[i].href + '">' + news[i].innerHTML + '</a></li>';
			}
			newslist +="</ul>";
			$("#eROC_Military_Command_list").html(newslist);
		});
	*/
	$('#eROC_legislative_News').html('eROC最新內政<br /><marquee id="eROC_legislative_News_list" onmouseover="this.stop()" onmouseout="this.start()" height="20" direction="up" scrolldelay="4" scrollamount="1"></marquee>');
	/*var militaryNews = $.get('/'+lang+'/newspaper/legislative-yuan-gazette-246398', function(data){
		var news = $('.articlecontent > .padded', data).contents();
		var newslist;
		newslist ="<ul>";
		for( var i=0;i<news.length;i++)
		{
			newslist += '<li><a href="' + news[i].href + '">' + news[i].innerHTML + '</a></li>';
		}
		newslist +="</ul>";
		$("#eROC_legislative_News_list").html(newslist);
	});
	*/

	//Add treasure map
	$("#menu2 a").after('<ul><li><a href="http://www.erepublik.com/'+lang+'/work">工作 Work</a></li>'+
						'<li><a href="http://economy.erepublik.com/'+lang+'/train">訓練 Train</a></li>'+
						'<li><a href="http://erep.tw">eROC台灣論壇</a></li>'+
						'<li><a href="http://qchat.rizon.net/?channels=taiwan">e台灣聊天頻道IRC Chat</a></li></ul>');
	//Add locations
	$("#menu2 li").eq(3).after('<li id="secondtier"><a href="http://qchat.rizon.net/?channels=#eROC-MoD">國防部頻道</a></li>'+
							   '<li id="secondtier"><a href="http://qchat.rizon.net/?channels=#eROC-MoI">移民局頻道</a></li>');
	$("#menu2 li").eq(2).after('<li id="secondtier"><a href="http://bit.ly/dQRjfl">國防部報紙</a></li>'+
							   '<li id="secondtier"><a href="http://bit.ly/hkfm2k">教育部報紙</a></li>'+
							   '<li id="secondtier"><a href="http://bit.ly/ic3sNq">立法院公報</a></li>');
							   
	$("#menu2 > ul > li > a").css({'background':'url("/images/parts/map-erepublik-logged.png") no-repeat scroll 0 -940px transparent', 'color':'#808080', 'display':'block', 'float':'left', 'font-size':'12px', 'height':'27px', 'padding':'13px 15px 0 28px', 'text-decoration':'none', 'width':'135px'});
	$("#menu2 > ul > li > a").hover(function(){
		this.style.background = 'url("/images/parts/map-erepublik-logged.png") no-repeat scroll 0 -899px transparent';
		this.style.color = '#FFFFFF';
		}, function() {
		this.style.background = 'url("/images/parts/map-erepublik-logged.png") no-repeat scroll 0 -940px transparent';
	this.style.color = '#808080';
		});

	$("li#secondtier").css({'padding-left':'15px'});
	$("li#secondtier > a").css({'padding-right':'5px', '-moz-border-radius':'5px', '-webkit-border-radius':'5px', 'border-radius':'5px', 'width':'125px'});
	$("li#secondtier > a").hover(function(){
	this.style.background = '#53b3d3';
	}, function() {
	this.style.background = 'url("/images/parts/map-erepublik-logged.png") no-repeat scroll 0 -940px transparent';
	});
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// load jQuery and execute the function
////////////////////////////////////////////////////////////////////////////////////////////////////
addJQuery(menuff);



//javascript:(function(){var subdomain = location.href.match(/(\w+)\.(\w+)/)[1];alert(subdomain)})();
//javascript:(function(){var ll =location.pathname.substr(1,2); alert(ll)})(); 
